

#include <string.h>
#include "BC26.h" 
#include "stdio.h" 
#include "usart.h"
#include "gpio.h"
#include "TelecomPlatform.h"
#include <string.h>

extern unsigned char UART1_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART1_Rx_cnt ;
extern unsigned char UART1_temp[REC_LENGTH];

extern  uint8_t Sent_Data[66];
extern uint8_t Simcard_exist;

uint8_t BC26_IMEI[IMEI_Len+1]={0};
uint8_t BC26_ICCID[ICCID_Len+1]={0};

uint8_t NB_NextSocketNumber=0 ;
void BC26_Power()
{
      SysPrintf("--->>BC26 power on...\n");
	    
	
    
			GPIO_InitTypeDef GPIO_InitStruct = {0};
			NB_PW_OFF;

			HAL_Delay(5000);
			NB_PW_ON;

	    
			BC26_EINT3V3_L;
			BC26_Reset_L;
			BC26_POWKEY_H ;
			HAL_Delay(1000);
		
			BC26_POWKEY_L ;
			HAL_Delay(500);
		
			BC26_Reset_H ;
			HAL_Delay(1000);
		
			BC26_Reset_L ;
			HAL_Delay(1000);	
}





void BC26_ATI_Status(void)
{
    uint8_t BC26_ATI_cmd[5+1]={'A','T','I','\r','\n',0};
    uint8_t i=10;
    SysPrintf("\n--->>>BC26_ATI_Status...\n");
    while(i--)
    {

        UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_ATI_cmd,5);
        HAL_Delay(2000);
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        

        
		


		if((strncmp(UART1_Rx_Buf,BC26_ATI_cmd,4)==0)&&(strstr(UART1_Rx_Buf,"OK")!=NULL))
        {
            SysPrintf("BC26_ATI_Status is OK\n");
            return ;
        }
        /*else 
        {
            
        }*/
        
    }

	SysPrintf("BC26_ATI_Status is ERR\n");

}


void BC26_CFUN(void)
{
	uint8_t BC26_CFUN_cmd[10+1]={'A','T','+','C','F','U','N','?','\r','\n'};  
    uint8_t i=5;
    SysPrintf("\n--->>>Set UE Functionality...\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_CFUN_cmd,10);
        HAL_Delay(1000);
        
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        
        
        if((strncmp(UART1_Rx_Buf,BC26_CFUN_cmd,9)==0)&&\
        (strstr(UART1_Rx_Buf,"OK")!=NULL))
        {
            SysPrintf("BC26_CFUN is OK\n");
            return ;
        }
        else 
        {
            SysPrintf("BC26_CFUN is ERR\n");
        }
        
    }
    
}


uint8_t BC26_Request_ICCID(void)
{
	uint8_t BC26_Check_CIMI_cmd[10+1]={'A','T','+','Q','C','C','I','D','\r','\n'};
    uint8_t i=5;
	 uint8_t j=0;
	uint8_t k=0;
    SysPrintf("--->>>Request_ICCID...\r\n");
    
	 UART1_Rx_cnt = 0;
    while(i--)
    {
       
        SendDataToNBModule(BC26_Check_CIMI_cmd,9);
        HAL_Delay(1000);
        SysPrintf("len=%d\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt] = 0;
				SysPrintf("%s\n",UART1_Rx_Buf);
        if(strstr(UART1_Rx_Buf,"OK")!=NULL)
        {
        	if(UART1_Rx_cnt>=30)
			{
				for(j=0;j<UART1_Rx_cnt-24;j++)
					if( (UART1_Rx_Buf[j]=='I') && (UART1_Rx_Buf[j+1]=='D') && (UART1_Rx_Buf[j+2]==':') && (UART1_Rx_Buf[j+24]=='\r') )
					{
						for(k=0;k<20;k++)
						{
							BC26_ICCID[k] = UART1_Rx_Buf[j+4+k];
							Sent_Data[k+23] = UART1_Rx_Buf[j+4+k];
						}
						SysPrintf("ICCID=%s\r\n",BC26_ICCID);
						return	TRUE;
				}
			}
		}		
     }
		SysPrintf("NO SIM Card\r\n");	 
    return FALSE ;
}

uint8_t BC26_Request_IMEI(void)
{
	uint8_t BC26_CGSN_cmd[11+1]={'A','T','+','C','G','S','N','=','1','\r','\n'};  
    uint8_t i=5;
	uint8_t j=0;
	uint8_t k=0;
    SysPrintf("Request IMEI...\r\n");
    
	UART1_Rx_cnt = 0;
    while(i--)
    {
        UART1_Rx_cnt = 0;
        SendDataToNBModule(BC26_CGSN_cmd,11);
        HAL_Delay(1000);
        SysPrintf("len=%d\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt] = 0;
		SysPrintf("%s\n",UART1_Rx_Buf);
        
        if(strstr(UART1_Rx_Buf,"OK")!=NULL)
        {
			if(UART1_Rx_cnt>=24)
			{
				for(j=0;j<UART1_Rx_cnt-19;j++)
					if( (UART1_Rx_Buf[j]=='S') && (UART1_Rx_Buf[j+1]=='N') && (UART1_Rx_Buf[j+2]==':') && (UART1_Rx_Buf[j+19]=='\r') )
					{
						for(k=0;k<15;k++)
						{
							BC26_IMEI[k] = UART1_Rx_Buf[j+4+k];
							Sent_Data[k+8] = UART1_Rx_Buf[j+4+k];
						}
					}
			}
			SysPrintf("BC26_IMEI=%s\r\n",BC26_IMEI);
			return TRUE ;
		}
		else 
		{
			SysPrintf("read IMEI error\r\n");
		}       
    }
    
    SysPrintf("read IMEI error\n");
    return FALSE ;
}

uint8_t BC26_Active_Net(void)
{
	uint8_t BC26_CGATT_cmd[12+1]={'A','T','+','C','G','A','T','T','=','1','\r','\n'};  	  
    uint8_t i=5;
    SysPrintf("\n--->>>BC26_Active_Net...\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_CGATT_cmd,12);
		HAL_Delay(2000);
		
		
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        
        
        if((strstr(UART1_Rx_Buf,"OK")!=NULL))
        {
            SysPrintf("BC26_Active_Net is OK\n");
            return TRUE;
        }
		else
		{
			HAL_Delay(3000);
		
		}
		
        
    }  
    SysPrintf("BC26_Active_Net is ERR\n");
    return FALSE ;
}
uint8_t Check_BC26_Net_Status(void)
{
	
	uint8_t BC26_Check_CGATT_cmd[11+1]={'A','T','+','C','G','A','T','T','?','\r','\n'};
    uint8_t i=5,result=0 ;
    SysPrintf("\n--->>>Check_BC26_Net_Status...\n");
    while(i--)
    {
        /*******************************************
    		Query whether the network is activated: 
    		1 means attached to network successfully, 
    		while 0 means has not been attached to network.
        *******************************************/
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_Check_CGATT_cmd,11);
        HAL_Delay(2000);
		
        
        
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        
        
        if((strncmp(UART1_Rx_Buf,BC26_Check_CGATT_cmd,10)==0)&&\
        (strstr(UART1_Rx_Buf,"OK")!=NULL))
        {
            result=UtilGetIntFlag(UART1_Rx_Buf,"+CGATT:","\r\n");
            SysPrintf("Check_BC26_Net_Status is OK=%d\n",result);
            return result ;
        }
        else 
        {
            SysPrintf("Check_BC26_Net_Status is ERR\n");
        }
    }
    
    return FALSE ;
}

void BC26_Check_Band(void)
{
    
	uint8_t BC26_Check_Band_cmd[11+1]={'A','T','+','Q','B','A','N','D','?','\r','\n'};
    uint8_t i=5;
    SysPrintf("\n--->>>Query the list of supported bands...\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_Check_Band_cmd,11);
        HAL_Delay(1000);
        
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        
        
        if((strncmp(UART1_Rx_Buf,BC26_Check_Band_cmd,10)==0)&&\
        (strstr(UART1_Rx_Buf,"OK")!=NULL))
        {
            SysPrintf("BC26_Check_Band is OK\n");
            return ;
        }
        else 
        {
            SysPrintf("BC26_Check_Band is ERR\n");
        }
    }
    
}

uint8_t BC26_Check_CESQ(void)
{
   
	uint8_t BC26_Check_CESQ_cmd[9+1]={'A','T','+','C','E','S','Q','\r','\n'};
    uint8_t i=5,j=0,result=0 ;
    SysPrintf("--->>>Query the signal strength...\r\n");
	UART1_Rx_cnt=0 ;
    while(i--)
    {
				
			
			
        SendDataToNBModule(BC26_Check_CESQ_cmd,9);
        HAL_Delay(2000);
        SysPrintf("len=%d\r\n",UART1_Rx_cnt);
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        SysPrintf("%s\n",UART1_Rx_Buf);
        
        
        
        
        if(strstr(UART1_Rx_Buf,"OK")!=NULL)
        {
            if(UART1_Rx_cnt>=25)
			{
				for(j=0;j<UART1_Rx_cnt-5;j++)
				{
					if((UART1_Rx_Buf[j]=='Q') && (UART1_Rx_Buf[j+1]==':') && (UART1_Rx_Buf[j+2]==' ') && (UART1_Rx_Buf[j+5]==','))
					{
						result = ((UART1_Rx_Buf[j+3]-0x30)<<3) + ((UART1_Rx_Buf[j+3]-0x30)<<1) + (UART1_Rx_Buf[j+4]-0x30);
					}
					else if((UART1_Rx_Buf[j]=='Q') && (UART1_Rx_Buf[j+1]==':') && (UART1_Rx_Buf[j+2]==' ') && (UART1_Rx_Buf[j+4]==','))
					{
						result = (UART1_Rx_Buf[j+3]-0x30);
					}
				}
			}
            SysPrintf("BC26_Check_CESQ is: %d\r\n",result);
            return result ;
        }
        else 
        {
            SysPrintf("BC26_Check_CESQ is ERR\n");
        }
    }
    return 0 ;
}

int BC26_Register_Net_Status(void)
{
    /*********************************************************
    AT+CEREG? ��ѯ NB ����ע��״̬����
    +CEREG: < n>,< state>,[,< lac>,< rac>,< ci>[,< AcT>]]
    ����˵����< n>:
    0 �ر�����ע��������ϱ������
    1 ������ע��������ϱ������+CREG:
    2 ������ע���λ����Ϣ�������ϱ������+CEREG: [,[,]]
    < state>:NB ע��״̬
    0 δע�ᣬMT ��ǰ��ǰû����������ע�ᵽ��Ӫ������
    1 ��ע�ᣬע�ᵽ��������
    2 δע�ᣬ���� MT ��ʱ���ڳ��Ը��Ż�����������ע�����Ӫ������
    3 ע�ᱻ�ܾ�
    4 δ֪����
    5 ��ע�ᣬע�ᵽ��������
    **********************************************************/
    
	uint8_t BC26_Register_Net_Status_cmd[11+1]={'A','T','+','C','E','R','E','G','?','\r','\n'};
    uint8_t i=1,j=0,result=0 ;
    
    SysPrintf("\n--->>>��ѯ NB ����ע��״̬\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
		SendDataToNBModule(BC26_Register_Net_Status_cmd,11);
        HAL_Delay(1000);
			HAL_Delay(1000);
		SysPrintf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        SysPrintf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			if(UART1_Rx_cnt>=29)
			{
				for(j=0;j<UART1_Rx_cnt-2;j++)
				{
					if( (UART1_Rx_Buf[j]==',') && (UART1_Rx_Buf[j+2]=='\r'))
					{
						result = UART1_Rx_Buf[j+1] -0x30;
						SysPrintf("BC26_Register_Net_Status is OK=%d\r\n",result);
						if(result==1)
						{
							return result ;
						}
					}
				}
			}
			SysPrintf("BC26_Register_Net_Status is OK\r\n"); 
			return TRUE;
		}
		else
		{
			 HAL_Delay(1000);
			 HAL_Delay(1000);
			 HAL_Delay(1000);
			 HAL_Delay(1000);
		}
	}
		SysPrintf("BC26_Register_Net_Status is ERR\r\n");
		return FALSE;   
    
}


uint8_t BC26_CSCON(void)
{
    
    
	uint8_t BC26_CSCON_cmd[11+1]={'A','T','+','C','S','C','O','N','?','\r','\n'};
    uint8_t i=5,result=0 ;
	uint8_t j=0;
    
    
    SysPrintf("\n--->>>��ѯ����״̬\r\n");
    while(i--)
    {
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(BC26_CSCON_cmd,11);
        HAL_Delay(1000);
			HAL_Delay(1000);
		SysPrintf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        SysPrintf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			if(UART1_Rx_cnt>=13)
			{
				for(j=0;j<UART1_Rx_cnt-2;j++)
				{
					if( (UART1_Rx_Buf[j]==',') && ((UART1_Rx_Buf[j+2]=='\r')) )
					{
						if((UART1_Rx_Buf[j+1]=='1'))
						{
							SysPrintf("Signalling Connection Status is Connetced\r\n");
						}
						else if((UART1_Rx_Buf[j+1]=='0'))
						{
							HAL_Delay(1000);
							HAL_Delay(1000);
							HAL_Delay(1000);
							HAL_Delay(1000);
							
						}
					}
				}					
			}
			SysPrintf("BC26_CSCON is OK\r\n"); 
			return TRUE;
		}
		else
		{
			SysPrintf("BC26_CSCON is ERR\r\n");
		}
		
	}
		SysPrintf("Signalling Connection Status is IDIE\r\n");
    return FALSE ;   
    
}

uint8_t BC26_COPS(void)
{
    /*************************************************************************
    AT+COPS
    ���ܣ�����ѡ����Ӫ���硣��Ҫ��2��ѡ��ʽ��
    1.�ֹ�ģʽ(manual): ����Ӧ�ó���ָ�������硣����ɹ��������ע�ᡣ
    2.�Զ�ģʽ��automatic���������������硣���ҵ��������ע�᣻���������������硣
    3.�ֹ�/�Զ�ģʽ��manual/automatic��������Ӧ�ó���ָ������·������ɹ�����ѡ��Ӧ�ó���ָ�����������ע�᣻���������Զ�ģʽ��������
    
    ����46000��ʾ��Ӫ�̱�ʶ��460��ʾ�й���00��ʾ�й��ƶ���01��ʾ�й���ͨ
    **************************************************************************/
    
	uint8_t BC26_COPS_cmd[10+1]={'A','T','+','C','O','P','S','?','\r','\n'};
	uint8_t BC26_COPS[5+1]={0};
    uint8_t i=5 ;
	uint8_t j=0 ;
    uint16_t result=0 ;
    uint8_t startflag[3]={',','"',0};
    uint8_t endflag[3]={'"',',',0};
    
    SysPrintf("\n--->>>��ѯ��Ӫ��\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
		SendDataToNBModule(BC26_COPS_cmd,10);
        HAL_Delay(1000);
		HAL_Delay(1000);
		SysPrintf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        SysPrintf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			if(strstr(UART1_Rx_Buf,"46000")!=NULL)
			{
				SysPrintf("�й��ƶ�\r\n");
				return 1;
			}
			else if(strstr(UART1_Rx_Buf,"46001")!=NULL)
			{
				SysPrintf("�й���ͨ\r\n");
				return 2;
			}
			else if(strstr(UART1_Rx_Buf,"46011")!=NULL)
			{
				SysPrintf("�й�����\r\n");
				return 3;
			}
			else
			{
				 HAL_Delay(1000);
				 HAL_Delay(1000);
				 HAL_Delay(1000);
				 HAL_Delay(1000);
			}
		}
		else
		{
			SysPrintf("BC26_COPS is ERR\r\n");
		}
		
	}
	SysPrintf("��ѯ������Ӫ��\r\n");
	return 0;	 
}

void BC26_PDPACT(void)
{
    
    
	uint8_t BC26_CGPADDR_cmd[14+1]={'A','T','+','C','G','P','A','D','D','R','=','1','\r','\n'};
    uint8_t i=5 ;
    SysPrintf("\n--->>>Get the IP address of the device...\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_CGPADDR_cmd,14);
        HAL_Delay(2000);
		
        
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        
        
        if((strstr(UART1_Rx_Buf,"OK")!=NULL))
        {
            SysPrintf("BC26_PDPACT is OK\n");
            i=0 ;
            return;
        }
        else 
        {
        	HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
        }
        
    }
	SysPrintf("BC26_PDPACT is ERR\n");
    
}

int8_t CreatSocket(uint8_t type,uint8_t	socketid,uint8_t*serveriP,uint8_t*serverport,uint8_t*localport)
{ 
	uint8_t j=0,sockedid=0,result=0 ;
    uint8_t ATStrBuf[50]={0};
	const uint8_t Buf4[6]={'"','U','D','P','"',0};
	const uint8_t Buf[6]={'"','T','C','P','"',0};
	const uint8_t Buf1[6]={',','"',0 };
	const uint8_t Buf2[6]={'"',',',0};
	const uint8_t Buf3[2]={',',0 };
    uint8_t*p=NULL ;
    SysPrintf("\n--->>>Connect Socket to Remote Address and Port...\n");
    
	sprintf(ATStrBuf,"AT+QIOPEN=%d,%d,",1,socketid);
    if(type==TCP_Connection)
    {
    	strcat(ATStrBuf,Buf);
    }
	else  if(type==UDP_Connection)
	{
		strcat(ATStrBuf,Buf4);
	}
    else
    {
		SysPrintf("socket type err\n");
	}
    strcat(ATStrBuf,Buf1);
    
    strcat(ATStrBuf,serveriP);
    
    strcat(ATStrBuf,Buf2);
    
    strcat(ATStrBuf,serverport);
    
    strcat(ATStrBuf,Buf3);
    
    strcat(ATStrBuf,localport);
    
    strcat(ATStrBuf,Buf3);
    
    strcat(ATStrBuf,"0,");
    
    strcat(ATStrBuf,"0");
    
    strcat(ATStrBuf,"\r\n\0");
    SysPrintf("ATStrBuf=%s\n",ATStrBuf);

        UART1_Rx_cnt=0 ;
        memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
        j=0 ;
        while(strstr(UART1_Rx_Buf,"+QIOPEN:")==NULL)
        {
            HAL_Delay(1000);
			
            if(++j>6)
            {
                break ;
            }
        }
		
        
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        
      
        if((strncmp(UART1_Rx_Buf,"AT+QIOPEN",strlen("AT+QIOPEN"))==0)&&\
        (strstr(UART1_Rx_Buf,"OK")!=NULL))
        {
            sockedid=UtilGetIntFlag(UART1_Rx_Buf,"+QIOPEN:",",");
            SysPrintf("socket %d\n",sockedid);
            p=strstr(UART1_Rx_Buf,"+QIOPEN:");
            if(p!=NULL)
            {
                result=UtilGetIntFlag(p,",","\r\n");
                SysPrintf("result %d\n",result);
                if(result==0)
                {
                    SysPrintf("creak socket %d is OK\n",sockedid);
                    
                    return sockedid ;
                }
            }
              
        }
        else 
        {
			SysPrintf("ERR\n");
        }
        
        
		
    
    return-1 ;
    
}

uint8_t	BC26_ConfigSleepMode(uint8_t		mode)
{

	uint8_t BC26_QSCLK0_cmd[14+1]={'A','T','+','Q','S','C','L','K','=','0','\r','\n'};
	uint8_t BC26_QSCLK1_cmd[14+1]={'A','T','+','Q','S','C','L','K','=','1','\r','\n'};
	uint8_t	buffer[20]={0};
	uint8_t i=5;
	if(mode==BC26_DisableSleepMode)
	{
		memcpy(buffer,BC26_QSCLK0_cmd,12);
	}
	else if(mode==BC26_EnableSleepMode)
	{
		memcpy(buffer,BC26_QSCLK1_cmd,12);
	}
	else
	{
		SysPrintf("BC26_ConfigSleepMode err\n");
		return FALSE;
	}
	SysPrintf("\n--->>>NB goto sleep %d\n",mode);
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(buffer,13);
		HAL_Delay(1000);
		
		
		
		
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		
		
		
		if((strncmp(UART1_Rx_Buf,"AT+QSCLK",strlen("AT+QSCLK"))==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26_ConfigSleepMode is OK\n");
			
			return TRUE;
		}
		
	}

	SysPrintf("BC26_ConfigSleepMode is ERR\n");
	return FALSE ;

}
uint8_t CloseSocket(uint8_t socketid)
{
    uint8_t i=3;
    uint8_t ATStrBuf[50]={0};
    SysPrintf("\n--->>>Close Socket...\n");
    
    sprintf(ATStrBuf,"AT+QICLOSE=%d\r\n",socketid);
    while(i--)
    {
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
        HAL_Delay(2000);
		
        
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        
        
        if((strncmp(UART1_Rx_Buf,"AT+QICLOSE",strlen("AT+QICLOSE"))==0)&&\
        (strstr(UART1_Rx_Buf,"OK")!=NULL))
        {
            SysPrintf("Close Socket	success\n");
            return TRUE ;
        }
        else 
        {
            SysPrintf("Close Socket	ERR\n");
        }
        
        
    }
    return FALSE ;
    
}

/***********
AT+QCSEARFCN
+QCSEARFCN: 0

OK

0 Successful
2 Failure
3 Not found
***********/
uint8_t BC26_QCSEARFCN(void)
{
	uint8_t BC26_CGPADDR_cmd[14+1]={'A','T','+','Q','C','S','E','A','R','F','C','N','\r','\n'};
	uint8_t i=3,result;
	SysPrintf("Clear NB-IoT Stored EARFCN List\r\n");
	UART1_Rx_cnt=0;
	while(i--)
	{
		
        SendDataToNBModule(BC26_CGPADDR_cmd,14);
        HAL_Delay(1000);
       
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        
        if((strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("AT+QCSEARFCN OK\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
		}
		SysPrintf("AT+QCSEARFCNT is ERR\n");
		return FALSE;
		}
}

void BC26_PowerStart(void)
{
    BC26_Power();
    BC26_ATI_Status();
    BC26_ConfigSleepMode(BC26_DisableSleepMode);
	
}


uint8_t BC26_PowerStartAndSleep(void)
{
    BC26_Power();
    BC26_ATI_Status();
    BC26_ConfigSleepMode(BC26_EnableSleepMode);
	
}


int8_t BC26_IntialAndCreatSocket(void)
{
	int8_t	socketid=-1;
	uint8_t	time=0,SignalStrength=0;
  BC26_Power();
  BC26_ATI_Status();	
	BC26_ConfigSleepMode(BC26_DisableSleepMode);
		
	BC26_QCSEARFCN();

	
	
	if(BC26_Request_ICCID()==FALSE)
	{
		Simcard_exist = 0x00;
		return	-1;
	}
	Simcard_exist = 0x01;
	
    BC26_Request_IMEI();

    while(1)
    {
    	if(++time>5)
    	{
			break;
		}
    	SignalStrength=BC26_Check_CESQ();
		if(SignalStrength<10)
		{
			HAL_Delay(2000);
		}
		else
		{
			break;
		}
    }
	
    if(BC26_Active_Net()==FALSE)
    {
		return	-1;
	}
	
    if(BC26_Register_Net_Status()==1)
    {
	    BC26_CSCON();
	    BC26_COPS();
	    BC26_PDPACT();
	}
	 
		
		BC26_QLWDEL();
	 
		if(BC26_QLWSERV(TelecomPlatform_IP,TelecomPlatform_port)==TRUE)
		{
			BC26_QLWCONF(BC26_IMEI);
			BC26_QLWADDOBJ();
		    if(BC26_QLWOPEN()==TRUE)
		    {
				BC26_QLWCFG();
				BC26_QLWUPDATE();
					
				
					
					
				return	0;
				
		    }
		}
	
	return	-1;

}

void Set_BC26_QATWAKEUP(void)
{
	uint8_t BC26_QATWAKEUP_cmd[16+1]={'A','T','+','Q','A','T','W','A','K','E','U','P','=','1','\r','\n',0}; 
  SysPrintf("\n--->>>BC26 set QATWAKEUP ...\n");
	UART1_Rx_cnt=0 ;
	SendDataToNBModule(BC26_QATWAKEUP_cmd,16);
	HAL_Delay(500);
	SysPrintf("len=%d\n",UART1_Rx_cnt);
	UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
	SysPrintf("%s\n",UART1_Rx_Buf);
	if(strstr(UART1_Rx_Buf,"OK")!=NULL)
	{
		 SysPrintf("BC26 set QATWAKEUP is OK\n");
		 return ;
	}
	SysPrintf("BC26 set QATWAKEUP is ERR\n");
}

uint8_t  CHeck_BC26_Wakeup_Sleep_Success(void) 
{
	uint8_t Get_BC26_QATWAKEUP_cmd[15+1]={'A','T','+','Q','A','T','W','A','K','E','U','P','?','\r','\n',0}; 
  SysPrintf("\n--->>>BC26 get QATWAKEUP ...\n");
	UART1_Rx_cnt=0 ;
	SendDataToNBModule(Get_BC26_QATWAKEUP_cmd,15);
	HAL_Delay(500);
	SysPrintf("len=%d\n",UART1_Rx_cnt);
	UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
	SysPrintf("%s\n",UART1_Rx_Buf);
	if(strstr(UART1_Rx_Buf,"OK")!=NULL)
	{
		 SysPrintf("BC26 get QATWAKEUP is OK\n");
		 return 1;
	}
	SysPrintf("BC26 get QATWAKEUP is ERR\n");
	return 0;
}

